<?php
final class HttpUtils {
	public static function doCurlPost($url, $params, $resultToJson = false) {
		$_curl = curl_init ();
		if (stripos ( $url, "https://" ) !== FALSE) {
			curl_setopt ( $_curl, CURLOPT_SSL_VERIFYPEER, FALSE );
			curl_setopt ( $_curl, CURLOPT_SSL_VERIFYHOST, false );
		}

		
		if (is_string ( $params )) {
			$_strPOST = $params;
		} else {
			$_strPOST = http_build_query ( $params );
		}
//        echo $url . PHP_EOL . PHP_EOL;
//        echo $_strPOST;
//        exit;

		curl_setopt ( $_curl, CURLOPT_URL, $url );
		curl_setopt ( $_curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $_curl, CURLOPT_POST, true );
		curl_setopt ( $_curl, CURLOPT_POSTFIELDS, $_strPOST );
		curl_setopt ( $_curl, CURLOPT_TIMEOUT, 5 );

		$_content = curl_exec ( $_curl );
		$_status = curl_getinfo ( $_curl );

        // 检查是否有错误发生
        if (curl_errno($_curl)) {
            self::setError(curl_errno($_curl), curl_error($_curl));
        }

		curl_close ( $_curl );
		
		if (intval ( $_status ["http_code"] ) == 200) {
			return $resultToJson ? json_decode ( $_content, true ) : $_content;
		}
		
		return false;
	}

	public static function setError($errno, $errmsg)
    {
        echo sprintf('Msg: %s; ErrNo: %s', $errmsg, $errno);
        exit;
    }
}